﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_maintain.TMS_repair
{
    public class TMS_repairs : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_repairs
    {

        public TMS_repairs(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        #region 油费显示
        public async Task<PageResponse<TMS_repairDto>> GetRep(string RepPeop, string RepNumber, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_repairDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_repairModel where 1=1 ";
            if (!string.IsNullOrEmpty(RepPeop))
            {
                sql += $" and RepPeop like '%{RepPeop}%'";
            }
            if (!string.IsNullOrEmpty(RepNumber))
            {
                sql += $" and RepNumber  like '%{RepNumber}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_repairDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_repairDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 油费添加
        public async Task<int> RepAdds(TMS_repairDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_repairModel values ('{info.Id}','{info.Reptitel}','{info.RepType}','{info.RepNumber}','{info.Repyuan}','{info.RepPeop}','{info.Repmiao}','{info.RepTim}','{info.RepTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 油费删除
        public async Task<int> DeletRep(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_repairModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

        #region 油费修改
        public async Task<int> UpdateRepup(TMS_repairDto tMS_Induction)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"update TMS_repairModel set Reptitel='{tMS_Induction.Reptitel}',RepType='{tMS_Induction.RepType}',RepNumber='{tMS_Induction.RepNumber}',Repyuan='{tMS_Induction.Repyuan}',RepPeop='{tMS_Induction.RepPeop}',Repmiao='{tMS_Induction.Repmiao}',RepTim='{tMS_Induction.RepTim}',RepTxt='{tMS_Induction.RepTxt}' where Id='{tMS_Induction.Id}'", null, await GetDbTransactionAsync());
        }
        #endregion
    }
}
